//
//  DrawOnMap.m
//  Mapping
//
//  Created by Ken Power on 20/08/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DrawOnMap.h"


@implementation DrawOnMap
@synthesize markerLocation;
@synthesize track;
@synthesize map;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque=NO;
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
	NSLog(@"drawRect");
	NSLog(@"Frame ox:%lf, oy%lf, w:%lf, h%lf",[self frame].origin.x,[self frame].origin.y,[self frame].size.width,[self frame].size.height);
	NSLog(@"bounds ox:%lf, oy%lf, w:%lf, h%lf",[self bounds].origin.x,[self bounds].origin.y,[self bounds].size.width,[self bounds].size.height);
	

    //draw the blue dot
	// Create an oval shape to draw.
    UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:
						   CGRectMake(0, 0, 25, 25)];
	
    // Set the render colors
    [[UIColor cyanColor] setStroke];
    [[UIColor blueColor] setFill];
	
	CGContextRef aRef=UIGraphicsGetCurrentContext();
	CGContextSaveGState(aRef);
	
    // Adjust the view's origin temporarily. The oval is
    // now drawn relative to the new origin point.
	CGPoint markerPos =[map GetPixelCoordsOnMap:[self frame] :markerLocation];

    CGContextTranslateCTM(aRef, markerPos.x,markerPos.y);
	
    // Adjust the drawing options as needed.
    aPath.lineWidth = 1;
	
    // Fill the path before stroking it so that the fill
    // color does not obscure the stroked line.
    [aPath fill];
    [aPath stroke];
	
    // Restore the graphics state before drawing any other content.
    CGContextRestoreGState(aRef);
	
	//[[UIColor greenColor] setStroke];
	
	CGMutablePathRef myPath=CGPathCreateMutable ();
	
	for(int i=0;i<[track count];i++){
		CLLocation *l=[track objectAtIndex:i];
		CGPoint p =[map GetPixelCoordsOnMap:[self frame] :l];
		
		if(i==0)
			CGPathMoveToPoint(myPath, NULL, p.x, p.y);
		else {
			CGPathAddLineToPoint(myPath, NULL, p.x, p.y);
		}
	}
		

	CGContextAddPath (aRef,myPath);
	
	CGContextStrokePath(aRef);
	
	CGMutablePathRef testPath=CGPathCreateMutable ();
	
	NSLog(@"drawRect2");
	
	NSLog(@"Frame ox:%lf, oy%lf, w:%lf, h%lf",[self frame].origin.x,[self frame].origin.y,[self frame].size.width,[self frame].size.height);
		
	
	CGPathMoveToPoint(testPath, NULL, 10, 10);
					
	CGPathAddLineToPoint(testPath, NULL, [self frame].size.width-10, 10);
	CGPathAddLineToPoint(testPath, NULL, [self frame].size.width-10, [self frame].size.height-10);
	CGPathAddLineToPoint(testPath, NULL, 10, [self frame].size.height-10);

	CGAffineTransform  t=self.transform ;
	NSLog(@"Transform %f %f",t.a,t.b);
	NSLog(@"Transform %f %f",t.c,t.d);
	NSLog(@"Transform %f %f",t.tx,t.ty);
	
	

	
	CGContextAddPath (aRef,testPath);
	
	CGContextStrokePath(aRef);
	
	
	
	
	
	////NSLog(@" x:%lf y%lf",lng,lat);
	//NSLog(@" w:%f h:%f",[self frame].size.width,[self frame].size.height);
	//NSLog(@" ox:%f oy:%f",[self frame].origin.x,[self frame].origin.y);
	
}

//- (void)setTransform:(CGAffineTransform)newValue {
//	//for (SubView *subView in self.subviews) {
//		self.transform = CGAffineTransformInvert(newValue);
//	//}
//	[super setTransform:newValue];
//}

- (void)dealloc {
    [super dealloc];
}


@end
